package com.apestech.api.condition;

import com.apestech.api.condition.Step.StepUtil;
import com.apestech.framework.json.JSON;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.xml.condition.modul.ConditionStrategy;

import java.util.ArrayList;

public class ConditionStrategyStepExec extends StepUtil {
    private ArrayList<ConditionStrategy.Step> steps;
    private JSONObject input;
    private JSONObject output;
    private ConditionStrategy.Step pstep;
    private ConditionStrategyExec conditionStrategyExec;


    public ConditionStrategyStepExec(ArrayList<ConditionStrategy.Step> steps, JSONObject input, ConditionStrategy.Step pstep, ConditionStrategyExec conditionStrategyExec){
        this.conditionStrategyExec=conditionStrategyExec;
        this.steps=steps;
        this.input=input;
        this.output=new JSONObject();
        this.pstep=pstep;
    }

    public JSONObject getOutput() {
        return output;
    }

    public void execStep() throws Exception {
        int len=steps.size();  //stepSize
        for(int i=0; i<len; i++) {
            ConditionStrategy.Step step = steps.get(i);
            JSONObject stepInp=new JSONObject();
            stepInp.putAll(input);
            if(pstep!=null){
                stepInp.putAll(pstep.getResultMap());
            }
            step.setSuperInput(stepInp);
            ConditionStrategySingleStepExec sexec=new ConditionStrategySingleStepExec(step, i, conditionStrategyExec);
            sexec.setStepInp(input);
            sexec.execSingleStep();
            if(sexec.isIsbreak()){
                output.putAll(sexec.getStepOut());
                break;
            }
            if(step!=null){
                System.out.println("#"+step.getId()+"#"+input.get("P001")+"#"+output.get("P001"));
//                if("P903".equals(step.getId())){   //CG04_02
//                    System.out.println("");
//                }
            }
            input.putAll(step.getResultMap());
            output.putAll(input);
//            System.out.println(JSON.toJSONString(sexec.getStepInfoList()));
            //stepInfoList=sexec.getStepInfoList();
        }
    }



}
